<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_User extends Controller_Admin_Based
{

    public function action_add()
    {
        if ($_POST)
        {
            $username = Arr::get($_POST, 'username', '');

            if (User::instance()->is_register($username))
            {
                Message::set('用户名已存在,请重现填写', 'notice');
                Request::current()->redirect(URL::base() . 'admin/user/add');
            }
            else
            {
                $user_id = User::instance()->register($_POST);

                if ($user_id)
                {
                    Message::set('添加成功');
                    Request::current()->redirect(URL::base() . 'admin/user/add');
                }
            }
        }

        $view = Viewer::display('admin/system/user_add')->render();
        $this->response->body($view);
    }

    public function action_list()
    {
        $pagination_arr = Paginationhelp::get_data('user', 'id_user');

        $data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/system/user_list', $data);
        $this->response->body($view);
    }

    public function action_edit()
    {
        $id_user = $this->request->param('id');

        $user = User::instance($id_user)->get();

        if ($_POST)
        {
            $password = Arr::get($_POST, 'password', '');
            if (!empty($password))
            {
                $data['password'] = toolkit::hash($password);
            }
            $data['email'] = Arr::get($_POST, 'email', '');
            $data['realname'] = Arr::get($_POST, 'realname', '');
            $data['username'] = Arr::get($_POST, 'username', '');
            $data['passwordhint'] = Arr::get($_POST, 'passwordhint', '');
            $data['telephone'] = Arr::get($_POST, 'telephone', '');
            $data['enabled'] = Arr::get($_POST, 'enabled', '');
            $data['edittime'] = time();

            $user = User::instance()->edit($id_user, $data);

            if ($user)
            {
                Message::set('管理员编辑成功');
                Request::current()->redirect(Url::base() . 'admin/user/list');
            }
            else
            {
                Message::set('管理员信息编辑失败,请检查参数', 'error');
                Request::current()->redirect(Url::base() . 'admin/user/edit/' . $id);
            }
        }

        $view = Viewer::display('admin/system/user_edit', array('data' => $user));
        $this->response->body($view);
    }

    public function action_delete()
    {
        $id_user = $this->request->param('id');
        $user = User::instance()->delete($id_user);

        if ($user)
        {
            Message::set('管理员删除成功');
            Request::current()->redirect(Url::base() . 'admin/user/list');
        }
        else
        {
            Message::set('管理员删除失败');
            Request::current()->redirect(Url::base() . 'admin/user/list');
        }
    }

   
    
     public function action_logout()
    {
        Session::instance()->delete('user_id');
        Request::current()->redirect(URL::base().'admin/login/login');
    }

}

